package com.wang.mandkey.activity;

import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.wang.mandkey.R;
import com.wang.mandkey.net.Connecter;
import com.wang.mandkey.net.ConnecterPool;
import com.wang.mandkey.net.INetCallBack;
import com.wang.mandkey.tool.RemoteOperateImpl;
import com.wang.mandkey.utils.ProjectEnvironment;

public class MenuAndTableActivity extends BaseActivity {
	
	public View mTableView=null;
	public boolean isAdd=false;
	
	private Button mButtonEnter;
	private Button mButtonClose;
	private Button mButtonRight;
	
	private RemoteOperateImpl  mRemoteOperateImpl;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Connecter conenctor = ConnecterPool.getConnectorByKey(ConnecterPool.STRING_CKEY);
		mRemoteOperateImpl=new RemoteOperateImpl(conenctor, new INetCallBack() {
			
			@Override
			public void OnStart() {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void OnIntercepted(String source) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void OnFinish() {
				// TODO Auto-generated method stub
				
			}
		});
	}
	@Override
	public boolean onMenuOpened(int featureId, Menu menu) {
		showFakeTitleBar();
		return super.onMenuOpened(featureId, menu);
	}
	
	@Override
	public void onOptionsMenuClosed(Menu menu) {
		hideFakeTitleBar();
		super.onOptionsMenuClosed(menu);
	}
	
	private void hideFakeTitleBar(){
		WindowManager windowmanager = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
		if(null!=mTableView){
			windowmanager.removeView(mTableView);
			isAdd=false;
		}
	}
	
	private void showFakeTitleBar(){
		 if(isAdd){
			 return;
		 }
		mTableView=View.inflate(this, R.layout.tab_view, null);
		mButtonClose=(Button)mTableView.findViewById(R.id.buttonClose);
		mButtonClose.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				sendTabCommand(ProjectEnvironment.STRING_COMMAND_CLOSE);
			}
		});
		
		mButtonEnter=(Button)mTableView.findViewById(R.id.buttonOpen);
		mButtonEnter.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sendTabCommand((ProjectEnvironment.STRING_COMMAND_ENTER));
			}
		});
		mButtonRight=(Button)mTableView.findViewById(R.id.buttonRight);
		mButtonRight.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sendTabCommand(ProjectEnvironment.STRING_COMMAND_RIGHT_CLICK);
			}
		});
		
		
		View view1 = getWindow().peekDecorView();
		WindowManager windowmanager = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
		android.view.WindowManager.LayoutParams layoutparams = new android.view.WindowManager.LayoutParams(
				WindowManager.LayoutParams.WRAP_CONTENT,
				WindowManager.LayoutParams.WRAP_CONTENT, 
				WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL, 
				WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE , 
				-1);
		layoutparams.gravity = 48;
		Rect rect = new Rect();
		view1.getWindowVisibleDisplayFrame(rect);
		int i = rect.top;
		layoutparams.y = i;
		windowmanager.addView(mTableView, layoutparams);
		isAdd=true;
	}
	
	public void sendTabCommand(String command){
		if(ProjectEnvironment.BOOLEAN_LOCK_KEYBOAED){
			this.mRemoteOperateImpl.sendCommand(command);
		}else{
			Toast.makeText(this, getResources().getString(R.string.mouse_activity_lock_first), Toast.LENGTH_SHORT).show();
		}
	}
}
